package com.truckdispatching.activity;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.truckdispatching.R;
import com.truckdispatching.util.ProvinceCityUtil;

public class ChoseProvinceActivity extends BaseActivity implements OnClickListener {
	private Button mBtnBack;
	private GridView mGridView;

	private ArrayList<String> mProvincesList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chose_province_activity);

		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setOnClickListener(this);

		mGridView = (GridView) findViewById(R.id.gridview);
		mProvincesList = ProvinceCityUtil.getProvinces();

		if (mProvincesList == null || mProvincesList.size() == 0) {
			try {
//				ProvinceCityUtil.importXml(getAssets().open("area.xml"));
				ProvinceCityUtil.importData();
				mProvincesList = ProvinceCityUtil.getProvinces();
			} catch (Exception e) {
			}
		}

		mGridView.setAdapter(new ProvinceAdapter(mProvincesList));
	}

	private class ProvinceAdapter extends BaseAdapter implements OnClickListener {
		private ArrayList<String> provinces = null;

		public ProvinceAdapter(ArrayList<String> provinces) {
			this.provinces = provinces;
		}

		@Override
		public int getCount() {
			if (provinces != null) {
				return provinces.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if (provinces != null && position < provinces.size()) {
				return provinces.get(position);
			}
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new Button(ChoseProvinceActivity.this);
				convertView.setBackgroundResource(R.drawable.btn_chose);
			}
			convertView.setOnClickListener(this);
			String length = provinces.get(position);
			((Button) convertView).setText(length);
			convertView.setTag(length);
			return convertView;
		}

		@Override
		public void onClick(View v) {
			String province = v.getTag().toString();
			ArrayList<String> cities = ProvinceCityUtil.getAllCitysByProvince(province);
			if(cities == null || cities.size() == 0){
				try {
//					ProvinceCityUtil.importXml(getAssets().open("area.xml"));
					ProvinceCityUtil.importData();
					mProvincesList = ProvinceCityUtil.getProvinces();
					cities = ProvinceCityUtil.getAllCitysByProvince(province);
				} catch (Exception e) {
				}
			}
			if (cities != null) {
				Intent intent = new Intent(ChoseProvinceActivity.this, ChoseCityActivity.class);
				intent.putExtra("province", province);
				intent.putStringArrayListExtra("cities", cities);
				startActivityForResult(intent, 1);
			} else {
				Intent data = new Intent();
				data.putExtra("result", province);
				setResult(100, data);
				finish();
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 100 && requestCode == 1) {
			setResult(100, data);
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		default:
			break;
		}
	}
}
